package com.itheima.aspects;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * @Author:篍
 * @Date: 2025/07/17 20:52
 * @Description:
 */
@Slf4j
//@Aspect
//@Component
public class AdviceTypeDemo {
    //抽取切入点表达式
    @Pointcut("execution(* com.itheima.service.*.*(..))")
    public void pt(){}

    //前置通知
    @Before("pt()")
    public void before(JoinPoint joinPoint){
        log.info("before");
    }

    //环绕通知
    @Around("pt()")
    public Object recordTime(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        log.info("around before");
        //执行原始方法
        Object result = proceedingJoinPoint.proceed();

        //原始方法如果有异常，后置代码不会执行
        log.info("around after");
        return result;
    }

    //后置通知，无论是否有异常都会执行
    @After("pt()")
    public void after(JoinPoint joinPoint){
        log.info("after ...");
    }

    //返回后通知（程序在正常执行的情况下，会执行的后置通知）
    @AfterReturning("pt()")
    public void afterReturning(JoinPoint joinPoint){
        log.info("afterReturning ...");
    }

    //异常通知，出现异常则执行后置通知
    @AfterThrowing("pt()")
    public void afterThrowing(JoinPoint joinPoint){
        log.info("afterThrowing ...");
    }
}
